package newPan3d.content.tree.luoye
{
	import _Pan3D.core.MathTween;
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.analysis.AnalysisServer;
	import _Pan3D.load.LoadInfo;
	import _Pan3D.load.LoadManager;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.geom.Vector3D;
	
	public class LuoYeDisplay3DSprite extends Display3DSprite
	{
		protected var lizhiVertexBuffer:VertexBuffer3D;
		protected var lizhiStateVertexBuffer:VertexBuffer3D;
		protected var lizhiIndexBuffer:IndexBuffer3D;
		protected var _program3D:Program3D;
		protected var _timer:Number=0;
		public function LuoYeDisplay3DSprite(context:Context3D)
		{
			super(context);
		}
		override public function update() : void {
			_context.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ONE);
			if (!this._visible) {
				return;
			}
			if (_objData && _objData.texture) {
				if(_program3D){
					_context.setProgram(_program3D);
				}else{
					_context.setProgram(this.program);
				}
				setVc();
				setVa();
				resetVa();
			}
		}
		
		override protected function uplodToGpu() : void {
			
			
			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			var inum:int=_objData.vertices.length/3;
			var xposition:Number;
			var yposition:Number;
			var zposition:Number;
			var wposition:Number;
			var angle:int = 0;
			for(var i:int;i<200;i++){
				vv=vv.concat(_objData.vertices);
				uu=uu.concat(_objData.uvs);
				xposition =Math.random()*100-50;
				yposition =Math.random()*1+0.5;
				zposition =Math.random()*100-50
				wposition =Math.random();
				for(var k:int=0;k<inum;k++){
					ss.push(xposition,yposition,zposition,wposition);// 倾斜
				}
				for(var j:int=0;j<_objData.indexs.length;j++){
					ii.push(_objData.indexs[j]+inum*i);
				}
				
			}
			_objData.vertexBuffer = this._context.createVertexBuffer(vv.length/ 3, 3);
			_objData.vertexBuffer.uploadFromVector(Vector.<Number>(vv), 0, vv.length / 3);
			
			_stateBuffer = this._context.createVertexBuffer(ss.length/ 4, 4); //存状态;
			_stateBuffer.uploadFromVector(Vector.<Number>(ss), 0, ss.length / 4);
			
			_objData.uvBuffer = this._context.createVertexBuffer(uu.length / 2, 2);
			_objData.uvBuffer.uploadFromVector(Vector.<Number>(uu), 0, uu.length / 2);
			
			_objData.indexBuffer = this._context.createIndexBuffer(ii.length);
			_objData.indexBuffer.uploadFromVector(Vector.<uint>(ii), 0, ii.length);
			
		}

		override public function set url(value : String) : void {
			_url = value;
			var loaderinfo : LoadInfo = new LoadInfo(Scene_data.fileRoot+"build/obj/"+ _url + ".obj", LoadInfo.XML, onObjLoad, false);
			LoadManager.getInstance().addSingleLoad(loaderinfo);
		}
		
		override protected function onObjLoad(str : String) : void {
			_objData = AnalysisServer.getInstance().analysisObj(str);
			TextureManager.getInstance().addTexture(Scene_data.fileRoot+"build/texture/" + _objData.mtl + ".jpg", addTexture, null);
		}

		override protected function setVa() : void {
			
			//_timer++;
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2, _stateBuffer, 0, Context3DVertexBufferFormat.FLOAT_4);
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		private var _stateBuffer:VertexBuffer3D;
		
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0,null);
		}
		
		override protected function setVc() : void {
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_timer++
			var t:Number=MathTween.easeInOut(_timer,0,360,50);
			t=_timer/200;
			//t=0.1
			var b:Number=0;
			var c:Number=0;
			var d:Number=1000;
			_context.setProgramConstantsFromVector(Context3DProgramType.VERTEX,8,Vector.<Number>( [t,b,c,d])); //这里暂可以传一个时间进去;

		}
		public function get program3D():Program3D
		{
			return _program3D;
		}
		public function set program3D(value:Program3D):void
		{
			_program3D = value;
		}
	
		
	}
}